{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Using TensorFlow backend.\n"
     ]
    }
   ],
   "source": [
    "from os import listdir\n",
    "from pickle import dump\n",
    "from keras.applications.vgg16 import VGG16\n",
    "from keras.preprocessing.image import load_img\n",
    "from keras.preprocessing.image import img_to_array\n",
    "from keras.applications.vgg16 import preprocess_input\n",
    "from keras.layers import Input, Reshape, Concatenate, Activation, Dense\n",
    "import keras\n",
    "import numpy as np\n",
    "import string\n",
    "# from progressbar import progressbar\n",
    "from keras.models import Model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "def extract_features():\n",
    "    # load model\n",
    "    model = VGG16()\n",
    "    model.layers.pop()\n",
    "    # extract final 49x512 conv layer for context vectors\n",
    "    final_conv = Reshape([49,512])(model.layers[-4].output)\n",
    "    model = Model(inputs=model.inputs, outputs=final_conv)\n",
    "    print(model.summary())\n",
    "    features = dict()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "dim_ctx = -1\n",
    "n_ctx = -1\n",
    "lstm_cell_dim = -1\n",
    "lstm_hidden_dim = -1\n",
    "\n",
    "# Initializes the cell & hidden states of the LSTM by passing the mean context vector through MLPs\n",
    "# [contexts] : n_ctx x dim_ctx matrix\n",
    "def init_lstm_states(contexts):\n",
    "    mean_context = tf.reduce_mean(contexts,)\n",
    "    \n",
    "    # num hidden units in MLP\n",
    "    mlp_hidden = 256\n",
    "    \n",
    "    # cell_state MLP\n",
    "    inputs_c = Input(shape=(dim_ctx,))\n",
    "    f_c = Dense(mlp_hidden,activation=\"relu\")(inputs_c)\n",
    "    f_c = Dense(lstm_cell_dim,activation=None)(f_c)\n",
    "    \n",
    "    # hidden_state MLP\n",
    "    inputs_h = Input(shape=(dim_ctx,))\n",
    "    f_h = Dense(mlp_hidden,activation=\"relu\")(inputs_h)\n",
    "    f_h = Dense(lstm_hidden_dim,activation=None)(f_h)\n",
    "    \n",
    "    return f_c,f_h\n",
    "\n",
    "# Computes alpha values (attention weights) by passing context vectors + hidden state through MLP\n",
    "# Includes hidden state by concatenating to end of alpha values\n",
    "def generate_alphas(contexts,hidden_state):\n",
    "    mlp_hidden = 256\n",
    "    \n",
    "    # tile and concatenate inputs\n",
    "    batch_size = contexts.shape[0]\n",
    "    tiled_hidden_state = tf.tile([[hidden_state]],[batch_size,n_ctx,1])\n",
    "    concat_input = Concatenate(axis=-1)((contexts,tiled_hidden_state))\n",
    "    \n",
    "    # feed into MLP\n",
    "    inputs = Input(shape=(dim_ctx+lstm_hidden_dim,))\n",
    "    x = Dense(mlp_hidden,activation=\"relu\")(inputs)\n",
    "    x = Dense(1,activation=None)(x)\n",
    "    x = Activation(\"softmax\")(x)\n",
    "    \n",
    "    return x\n",
    "\n",
    "# Generates a soft-attention attention vector from alphas & context vectors\n",
    "def get_soft_attention_vec(contexts,alphas):\n",
    "    return contexts*tf.reshape(alphas,[1,-1,1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "alphas = np.random.randint(10,size=[49])\n",
    "contexts = np.ones([10,49,512])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(10, 49, 512)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(contexts*np.reshape(alphas,[1,-1,1])).shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "dim_ctx = 10\n",
    "lstm_hidden_dim = 10\n",
    "mlp_hidden = 1\n",
    "inputs = Input(shape=(dim_ctx+lstm_hidden_dim,))\n",
    "x = Dense(mlp_hidden,activation=\"relu\")(inputs)\n",
    "x = Dense(1,activation=None)(x)\n",
    "x = Activation(\"softmax\")(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tf.Tensor 'activation_1/Softmax:0' shape=(?, 1) dtype=float32>"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Anaconda",
   "language": "python",
   "name": "anaconda"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
